﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace PingYou.Console
{
    /// <summary>
    /// 通用数据访问类
    /// </summary>
    public class SQLHelper
    {
        //获取配置文件
        static string connString = ConfigurationManager.ConnectionStrings["connString"].ToString();

        //适用于insert update delete
        public static int ExacuteNoQuery(string sql)
        {
            int result = -1;
            //【1】新建数据库连接
            SqlConnection conn = new SqlConnection(connString);
            //【2】将sql写入命令
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                //【3】打开数据库连接
                conn.Open();
                //【4】执行sql
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            { 
            
            }
            finally
            {
                //【5】关闭连接
                conn.Close();
            }
            return result;





        }





        #region 执行插入操作
        public static void ExecuteInsert()
        {
            string sql = @"create table Student (
 ID int NOT NULL,
 StudentName NVARCHAR(100),
 Age int,
 Gender int ,
 Remark NVARCHAR(500),
 Enabled int ,
 CreaetStaff INT,
 CreateDate datetime,
 ModifyStaff INT,
 ModifyDate datetime)";
            int result = SQLHelper.ExacuteNoQuery(sql);
        }
        #endregion
    }
}
